/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.atp.agencia.principal;

import br.com.atp.agencia.controle.Pousada;
import br.com.atp.agencia.frame.WindowEventHandler;
import br.com.atp.agencia.modelo.Aluguel;
import br.com.atp.agencia.modelo.Chale;
import br.com.atp.agencia.modelo.Quarto;
import br.com.atp.agencia.modelo.QuartoBanheira;
import br.com.atp.agencia.ui.JIFPropriedadesSistema;
import br.com.atp.agencia.ui.JIFPropriedadesSistema;
import br.com.atp.agencia.ui.JIlFEscolheAcomodacao;
import br.com.atp.agencia.ui.JIlFEscolheAcomodacao;
import br.com.atp.agencia.ui.controle.aluguel.ControleChale;
import br.com.atp.agencia.ui.controle.aluguel.ControleQuarto;
import br.com.atp.agencia.ui.controle.aluguel.ControleQuartoBanheira;
import br.com.atp.agencia.ui.controle.aluguel.JIFControlaAluguel;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.event.AncestorListener;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

/**
 *
 * @author Afonso
 */
public class JPrincipal extends javax.swing.JFrame {

    public static Aluguel aluguelTrabalhado;
    private Pousada p;
    boolean registrar;

    public JPrincipal(Pousada p) {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.p = p;
        informarValorDiaria();
    }

    public JPrincipal() {
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem5 = new javax.swing.JMenuItem();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        pneAreaTrabalho = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenuRegistraEntrada = new javax.swing.JMenuItem();
        jMenuRegistraSaida = new javax.swing.JMenuItem();
        jMenuAtualizaValorDiaria = new javax.swing.JMenuItem();
        jMenuSair = new javax.swing.JMenuItem();
        JMenuPropriedadesSistema = new javax.swing.JMenu();
        jMenuPropriedadeSistema = new javax.swing.JMenuItem();

        jMenuItem5.setText("jMenuItem5");

        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("jRadioButtonMenuItem1");

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu2.setText("Menu");

        jMenuRegistraEntrada.setText("Registrar entrada em quarto");
        jMenuRegistraEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuRegistraEntradaActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuRegistraEntrada);

        jMenuRegistraSaida.setText("Registrar Saída do quarto");
        jMenuRegistraSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuRegistraSaidaActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuRegistraSaida);

        jMenuAtualizaValorDiaria.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuAtualizaValorDiaria.setText("Atualizar o valor da diária");
        jMenuAtualizaValorDiaria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuAtualizaValorDiariaActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuAtualizaValorDiaria);

        jMenuSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMenuSair.setText("Sair");
        jMenuSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuSairActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuSair);

        jMenuBar1.add(jMenu2);

        JMenuPropriedadesSistema.setText("Funções Adicionais");
        JMenuPropriedadesSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JMenuPropriedadesSistemaActionPerformed(evt);
            }
        });

        jMenuPropriedadeSistema.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuPropriedadeSistema.setText("Propriedades do Sistema");
        jMenuPropriedadeSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPropriedadeSistemaActionPerformed(evt);
            }
        });
        JMenuPropriedadesSistema.add(jMenuPropriedadeSistema);

        jMenuBar1.add(JMenuPropriedadesSistema);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pneAreaTrabalho, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pneAreaTrabalho, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuSairActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jMenuSairActionPerformed

    private void jMenuRegistraEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuRegistraEntradaActionPerformed
        // TODO add your handling code here:
        escolheAcomodacao();
        registrar = true;
    }//GEN-LAST:event_jMenuRegistraEntradaActionPerformed

    private void jMenuRegistraSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuRegistraSaidaActionPerformed
        // TODO add your handling code here:
        escolheAcomodacao();
        registrar = false;
    }//GEN-LAST:event_jMenuRegistraSaidaActionPerformed

    private void jMenuAtualizaValorDiariaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuAtualizaValorDiariaActionPerformed
        // TODO add your handling code here:
        atualizaValorDiarias();
    }//GEN-LAST:event_jMenuAtualizaValorDiariaActionPerformed

    private void JMenuPropriedadesSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JMenuPropriedadesSistemaActionPerformed
        this.mostrarInternalFrame(new JIFPropriedadesSistema());
    }//GEN-LAST:event_JMenuPropriedadesSistemaActionPerformed

    private void jMenuPropriedadeSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPropriedadeSistemaActionPerformed
        // TODO add your handling code here:
        this.mostrarInternalFrame(new JIFPropriedadesSistema());
    }//GEN-LAST:event_jMenuPropriedadeSistemaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu JMenuPropriedadesSistema;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuItem jMenuAtualizaValorDiaria;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuPropriedadeSistema;
    private javax.swing.JMenuItem jMenuRegistraEntrada;
    private javax.swing.JMenuItem jMenuRegistraSaida;
    private javax.swing.JMenuItem jMenuSair;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JDesktopPane pneAreaTrabalho;
    // End of variables declaration//GEN-END:variables

    private void mostrarInternalFrame(JInternalFrame frame) {
        this.pneAreaTrabalho.add(frame);
        frame.setVisible(true);
    }

    private void atualizaValorDiarias() {
        int op;
        op = JOptionPane.showConfirmDialog(rootPane, "Deseja atualiza o valor da Diaria? \n valor antigo: " + Aluguel.getValorDiaria());
        if (op == 0) {
            informarValorDiaria();
        } else {
            obrigado();
        }
    }

    private void obrigado() {
        JOptionPane.showMessageDialog(rootPane, "Obrigado!");
    }

    private void informarValorDiaria() {
        Double valorNovo;
        try {
            valorNovo = Double.parseDouble(JOptionPane.showInputDialog(rootPane, "Informe o novo valor para a Diaria."));
            Aluguel.setValorDiaria(valorNovo);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(rootPane, "Valor informado é incorreto!\n Valor não atualizado", null, JOptionPane.ERROR_MESSAGE);
        } finally {
            obrigado();
        }
    }

    private void escolheAcomodacao() {
        JIlFEscolheAcomodacao jIlFEscolheAcomodacao = new JIlFEscolheAcomodacao(p);
        mostrarInternalFrame(jIlFEscolheAcomodacao);
        jIlFEscolheAcomodacao.addInternalFrameListener(listenerInternalFrame);
    }

    private boolean validaEscolha() {
        if (aluguelTrabalhado.isOcupado()) {
            return false;
        } else {
            return true;
        }
    }

    private void quartoIndisponivel() {
        JOptionPane.showMessageDialog(rootPane, "Quarto indisponivel para opcao desejada!");
    }
    InternalFrameListener listenerInternalFrame = new InternalFrameListener() {
        @Override
        public void internalFrameActivated(InternalFrameEvent e) {
            dumpInfo("Activated", e);
        }

        @Override
        public void internalFrameClosed(InternalFrameEvent e) {
            dumpInfo("Closed", e);
        }

        @Override
        public void internalFrameClosing(InternalFrameEvent e) {
            dumpInfo("Closing", e);
        }

        @Override
        public void internalFrameDeactivated(InternalFrameEvent e) {
            dumpInfo("Deactivated", e);
            if (registrar && validaEscolha()) {
                direcionarAlugel(aluguelTrabalhado, registrar);
            } else if (!registrar && !validaEscolha()) {
                direcionarAlugel(aluguelTrabalhado, registrar);
            } else {
                quartoIndisponivel();
            }

        }

        @Override
        public void internalFrameDeiconified(InternalFrameEvent e) {
            dumpInfo("Deiconified", e);
        }

        @Override
        public void internalFrameIconified(InternalFrameEvent e) {
            dumpInfo("Iconified", e);
        }

        @Override
        public void internalFrameOpened(InternalFrameEvent e) {
            dumpInfo("Opened", e);
        }

        private void dumpInfo(String s, InternalFrameEvent e) {
            System.out.println("Source: " + e.getInternalFrame().getName()
                    + " : " + s);
        }

        private void direcionarAlugel(Aluguel aluguelTrabalhado, boolean registrar) {
            if (aluguelTrabalhado instanceof Quarto) {
                mostrarInternalFrame(new ControleQuarto((Quarto) aluguelTrabalhado, registrar));
            } else if (aluguelTrabalhado instanceof QuartoBanheira) {
                mostrarInternalFrame(new ControleQuartoBanheira((QuartoBanheira) aluguelTrabalhado, registrar));
            } else if (aluguelTrabalhado instanceof Chale) {
                mostrarInternalFrame(new ControleChale((Chale) aluguelTrabalhado, registrar));
            }
        }
    };
}
